﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
using HKEA.CMS.Core.Utility;

namespace HKEA.CMS.Core.BLL.Mtbl
{
    public class mtblAttributeBLL
    {
        private ImtblAttribute IDAL;
        public mtblAttributeBLL()
        {
            IDAL = DataAccess.CreateImtblAttribute();
        }

        public static mtblAttributeBLL GetInstance()
        {
            return new mtblAttributeBLL();
        }
        /// <summary>
        /// 插入一条记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int InsertENT(mtblAttribute ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret= IDAL.InsertENT(ent);
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
            return ret;
        }

        public string GetAttrubuteByUID(List<int> listUID)
        {
            string str = string.Empty;
            StringBuilder strBulilder = new StringBuilder();
            if (listUID.Count() > 0)
            {
                try {
                    var ret = IDAL.GetListByUIDList(listUID);
                    string strSpace = ",&nbsp;";
                    if (ret.Count > 0)
                    {
                        foreach (var temp in ret)
                        {
                            strBulilder.Append(temp.Attribute_Eng);
                            if (!string.IsNullOrEmpty(temp.Attribute_Chi))
                            {
                                strBulilder.Append(" ");
                                strBulilder.Append(temp.Attribute_Chi);
                            }
                            strBulilder.Append(strSpace);
                        }
                    }
                    str = strBulilder.ToString();
                    if (str.Length > 0)
                        str = str.Substring(0, str.Length - strSpace.Length);
                }
                catch (Exception)
                { throw; }
            }
            return str;
        }
        /// <summary>
        /// 物理删除
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int DeleteENT(mtblAttribute ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.DeleteENT(ent);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return ret;
        }
        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int UpdateENT(mtblAttribute ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.UpdateENT(ent);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return ret;
        }
        /// <summary>
        /// 根据条件得到符合条件的结果集，带分页参数
        /// </summary>
        /// <param name="ent"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<mtblAttribute> GetListByENT(mtblAttribute ent,PageParam param, string strPropertyName,string strOrderBy)
        {
            List<mtblAttribute> list=new List<mtblAttribute>();
            if (ent != null && param != null)
            {
                try
                {
                    list= IDAL.GetListByENT(ent,param,strPropertyName,strOrderBy);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return list;
        }

        public List<AttributeDetail> GetAllByENT(mtblAttribute ent)
        {
            List<AttributeDetail> list = new List<AttributeDetail>();
            if (ent != null)
            {
                try { list = IDAL.GetAllByENT(ent); }
                catch (Exception er)
                { }
            }
            return list;
        }
        /// <summary>
        /// 根据条件得到符合条件的结果集，不带分页参数
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public List<mtblAttribute> GetListByENT(mtblAttribute ent)
        {
            List<mtblAttribute> list = new List<mtblAttribute>();
            if (ent != null)
            {
                try
                {
                    list = IDAL.GetListByENT(ent);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return list;
        }
        /// <summary>
        /// 根据条件得到符合条件的记录条数
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int GetCountByENT(mtblAttribute ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.GetCountByENT(ent);
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
            return ret;
        }
        /// <summary>
        /// 判断是否已存在符合条件的记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public bool IsExist(mtblAttribute ent)
        {
            bool ret = false;
            if (ent != null)
            {
                if (GetCountByENT(ent) > 0)
                    ret = true;
            }
            return ret;
        }
    }
}
